package com.codbking.calendar.exaple.richeditor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CircleView extends View {
    private int radius = 20;
    private int loopRadius;
    private Paint paint;
    private Paint loopPaint;
    private Paint borderPaint;
    private int loopColor = 0xff0099cc;
    private int borderColor = 0xffDCDCDC;
    private boolean isSelect = false;

    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        loopPaint = new Paint();
        loopPaint.setAntiAlias(true);
        loopPaint.setStyle(Paint.Style.STROKE);
        loopPaint.setColor(loopColor);
        loopPaint.setStrokeWidth(4);

        borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        radius = MeasureSpec.getSize(widthMeasureSpec) / 2;
        loopRadius = radius - 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(radius, radius, radius, paint);
        canvas.drawCircle(radius, radius, radius - 1, borderPaint);
        if (isSelect) {
            canvas.drawCircle(radius, radius, loopRadius, loopPaint);
        }
    }

    public void setColor(int color) {
        if (color != 0) {
            paint.setShader(null);
            paint.setColor(color);
        } else {
            int[] mark = new int[]{0xFFFF0000, 0xFF00FF00, 0xFF0000FF};
            Shader shader = new LinearGradient(0, 0, radius * 4, 0, mark, null, Shader.TileMode.CLAMP);
            paint.setShader(shader);
        }
        invalidate();
    }

    public void select(boolean isSelect) {
        this.isSelect = isSelect;
        invalidate();
    }
}
